package com.xiegang.serviceMap.remote;

import com.google.gson.JsonObject;
import com.xiegang.internalcommon.constant.AmapConfigConstants;
import com.xiegang.internalcommon.response.DriectionResponse;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author xieGang
 * @description
 * @date 2023/3/12 16:11
 **/
@Service
@Slf4j
public class AmapServiceClient {

    @Value("${Amap.key}")
    private String key;

    @Autowired
    private RestTemplate restTemplate;

    public DriectionResponse driection(String depLongitude,String deplatitude,String destLongitude,String destLatiude){

        /***
         * https://restapi.amap.com/v3/direction/driving?origin=116.481028,39.989643&destination=116.465302,40.004717&extensions=all&output=json&key=d5df9c3cb246a0dff2969b5ba9e336eb
         */
        StringBuilder driectionUrl = new StringBuilder();
        driectionUrl.append(AmapConfigConstants.DRIECTION_URL);
        driectionUrl.append("?");
        driectionUrl.append("origin="+depLongitude+","+deplatitude);
        driectionUrl.append("&");
        driectionUrl.append("destination="+destLongitude+","+destLatiude);
        driectionUrl.append("&");
        driectionUrl.append("extensions=base");
        driectionUrl.append("&");
        driectionUrl.append("output=json");
        driectionUrl.append("&");
        driectionUrl.append("key="+key);

        ResponseEntity<String> forEntity = restTemplate.getForEntity(driectionUrl.toString(), String.class);

        log.info(forEntity.getBody());

        DriectionResponse driectionResponse = parseDirectionEntity(forEntity.getBody());

        return driectionResponse;
    }

    public DriectionResponse parseDirectionEntity(String directionString){
        DriectionResponse driectionResponse = null;

        try {
            //最外层
            JSONObject result = JSONObject.fromObject(directionString);
            if(result.has(AmapConfigConstants.STATUS)){
                int status = result.getInt(AmapConfigConstants.STATUS);
                if(status == 1){
                    if(result.has(AmapConfigConstants.ROUTE)){
                        JSONObject routeObject = result.getJSONObject(AmapConfigConstants.ROUTE);
                        JSONArray pathsArray = routeObject.getJSONArray(AmapConfigConstants.PATHS);
                        JSONObject pathObject = pathsArray.getJSONObject(0);

                        driectionResponse = new DriectionResponse();

                        if(pathObject.has(AmapConfigConstants.DISTANCE)){
                            int distance = pathObject.getInt(AmapConfigConstants.DISTANCE);
                            driectionResponse.setDistance(distance);
                        }
                        if(pathObject.has(AmapConfigConstants.DURATION)){
                            int duration = pathObject.getInt(AmapConfigConstants.DURATION);
                            driectionResponse.setDuration(duration);
                        }
                    }

                }
            }


        }catch (Exception e){

        }

        return driectionResponse;
    }
}
